package org.yx.service.sbas.save_production_line;

import org.yx.exception.BizException;
import org.yx.models.SbasLinDef;
import org.yx.models.SbasWspDef;
import org.yx.utils.BaseService;
import org.yx.utils.FieldMap;

import com.xiaoleilu.hutool.util.StrUtil;

/**
 * 保存生产线信息
 * @author zxw
 * @since 2018/04/14
**/
public class SbasSaveProductionLineService extends BaseService {
	public SbasSaveProductionLineOut saveProductionLine(SbasSaveProductionLineIn in) throws Exception {
		if (StrUtil.isBlank(in.getLineCode())) {
			throw new BizException("SYS-0004", new FieldMap("parameter", "lineCode"));
		}

		if (!StrUtil.isBlank(in.getShopCode())) {
			SbasWspDef shop = selectOne(SbasWspDef.class, in.getUserOrgCode(), in.getShopCode());

			if (shop == null || new Character('D').equals(shop.getStatus())) {
				throw new BizException("SBAS-0004", new FieldMap("shopCode", in.getShopCode()));
			}
		}

		SbasLinDef oldLine = selectOne(SbasLinDef.class, in.getUserOrgCode(), in.getLineCode());
		SbasLinDef line = oldLine == null ? new SbasLinDef() : oldLine;
		if (new Character('D').equals(line.getStatus())) {
			throw new BizException("SBAS-0005", new FieldMap("lineCode", line.getLineCode()));
		}
		copyProperties(in, line);

		if (new Character('D').equals(in.getStatus())) {
			if (oldLine == null) {
				throw new BizException("SBAS-0005", new FieldMap("lineCode", line.getShopCode()));
			}
			line.setStatus(in.getStatus());

			update(line, "status");
		} else {
			line.setStatus('N');

			upsert(line);
		}

		SbasSaveProductionLineOut out = new SbasSaveProductionLineOut();

		return out;
	}
}